package cn.tedu.weibo.service.impl;

import cn.tedu.weibo.exception.ServiceException;
import cn.tedu.weibo.mapper.CommentMapper;
import cn.tedu.weibo.pojo.dto.CommentDTO;
import cn.tedu.weibo.pojo.entity.Comment;
import cn.tedu.weibo.pojo.vo.CommentVO;
import cn.tedu.weibo.response.StatusCode;
import cn.tedu.weibo.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public void addNew(CommentDTO commentDTO) {
        Comment comment =new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        int rows = commentMapper.insert(comment);
        if (rows==0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
    }

    @Override
    public List<CommentVO> listByWeiboId(Long id) {
        return commentMapper.selectByWeiboId(id);
    }
}
